// 表格数据展示
$(function(){

  // 获取所有试验方案
  $.ajax({
    type:"post",
    url:"/sy/pm/findPageTPM",
    success:function(result){

        // console.log("result",result.data)

        var str = "";
        $.each(result.data,function(index,item){
              str+="<tr>"+
              "<td>"+(index+1)+"</td>"+
              "<td>"+item.testName+"</td>"+
              "<td>"+item.testType+"</td>"+
              "<td><button style='margin-right:15px;' onclick='addPoint("+item.id+")'>添加节点</button> <button style='margin-right:15px;' onclick='findPoint("+item.id+")'>查看节点</button> <button style='margin-right:15px;' onclick=''>复制节点</button></td>"+
          "</tr>"
        })
        $(".syTable tbody").append(str);
    }
  });

})

// 点击--添加节点按钮
function addPoint(pId){
  // 试验任务的id
  var taskId = pId;
  console.log("任务id",taskId);
  layer.open({
    type: 1 //Page层类型
    ,area: ['1400px', '700px']
    ,title: "添加节点具体信息"
    ,shade: 0.4 //遮罩透明度
    ,maxmin: false //允许全屏最小化
    ,anim: 1 //0-6的动画形式，-1不开启
    ,content: `
      <div id="pointArea">

          <div class="layui-form col-md-6 col-lg-6" style=";float:left;">
            <div class="layui-form-item">
                <div class="layui-inline">
                  <label class="layui-form-label">节点名称:</label>
                  <div class="layui-input-inline">
                  <input type="text" name="inputInterface" placeholder="请输入节点名称" class="layui-input">
                  </div>
                </div>
            </div>
          </div>

          <div class="layui-form col-md-6 col-lg-6" style=";float:left;">
            <div class="layui-form-item">
                <div class="layui-inline">
                  <label class="layui-form-label">节点日期:</label>
                  <div class="layui-input-inline">
                    <input type="text" class="layui-input" id="test5" placeholder="请选择节点日期">
                  </div>
                </div>
            </div>
          </div>

      </div>  
      <button class="btn btn-primary btn-size col-xs-3 col-sm-3 col-md-3 col-lg-3 col-xl-3" id="addNode" style="float:left;" onclick="addPointMessage()">添加节点</button>
      <button class="btn btn-secondary col-xs-3 col-sm-3 col-md-3 col-lg-3 col-xl-3 btn-size" id="saveNode" style="float:left;" onclick="savePointMessage()">保存节点</button>
      
    `
    ,success: function(){
        //日期时间选择器
        layui.use('laydate', function(){
          var laydate = layui.laydate;

            laydate.render({
              elem: '#test5'
              ,type: 'datetime'
            });

        })

      // 保存节点
        $("#saveNode").click(function(){
            // alert(taskId)
              // 获取节点名字
            var nodeName = $(".layui-input").val();
            var plannedEndTime = $("#test5").val();

            var dataArr =[
              {
                "nodeName": nodeName,
                "plannedEndTime": plannedEndTime,
                "pid": taskId
              }
            ];
            // 添加节点
            $.ajax({
                type:"post",
                url:"/sy/pm/addTestNode",
                dataType: 'json',
                contentType: 'application/json',
                data:JSON.stringify(dataArr),
                success:function(res){

                  if(nodeName===null||plannedEndTime===null){
                    layer.msg("请填写节点日期、节点名称");
                  }else{
                      if(res.status===0){
                        layer.msg("节点添加成功");
                        setTimeout(function(){
                              layer.closeAll();
                            },500)
                      }else{
                        layer.msg("节点添加失败，请重新添加！");
                        setTimeout(function(){
                          layer.closeAll();
                        },500)
                      }
                  }
                     
                }

            }) 
        })


    }
  })

}
//增加多个节点
function addPointMessage(){

  var str1 = "";
  str1+= "<div class='layui-form col-md-6 col-lg-6' style=';float:left;'>"+
            "<div class='layui-form-item'>"+
                "<div class='layui-inline'>"+
                 " <label class='layui-form-label'>节点名称:</label>"+
                  "<div class='layui-input-inline'>"+
                   " <input type='text' name='inputInterface' placeholder='请输入节点名称' class='layui-input'>"+
                  "</div>"+
                "</div>"+
            "</div>"+
          "</div>"+
          "<div class='layui-form col-md-6 col-lg-6' style='float:left;'>"+
              "<div class='layui-form-item'>"+
                " <div class='layui-inline'>"+
                  "  <label class='layui-form-label'>节点日期:</label>"+
                    "<div class='layui-input-inline'>"+
                      "<input type='text' class='layui-input' id='test5' placeholder='请选择节点日期'>"+
                    "</div>"+
                " </div>"+
              "</div>"+
       " </div>"
  $("#pointArea").append(str1);

}

// 点击查看节点信息
function findPoint(ID){
  //试验任务id
  var pId = ID;
  console.log("id",pId);
  $.ajax({
      type:"post",
      url:"/sy/pm/viewProcess",
      data:{pId:pId},
      success:function(result){

          console.log("result",result.data)

          var str = "";
          $.each(result.data,function(index,item){
            
            str+="<div class='nodeMessage' style='width:100%;height: 78px;margin-top: 10px;background-color: #ffffff;box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.05);'>"+
                      "<h4 style='display: inline-block;width: 114px;float: left;margin-top: 26px;margin-left: 20px;'>节点具体信息：</h4>"+
                      "<div class='col-md-2 col-lg-2' style='float:left;'>"+
                            "<div class='form-group'>"+
                            " <label for='inputInterface'>节点名称：</label>"+
                            " <input type='text' name='inputInterface'  class='form-control' value="+item.nodeName+">"+
                            "</div>"+
                      "</div>"+
                      "<div class='col-md-2 col-lg-2' style='float:left;'>"+
                          " <div class='form-group'>"+
                              "<label for='inputInterface'>计划完成时间：</label>"+
                              "<input type='text' name='inputInterface'  class='form-control' value="+item.plannedEndTime+">"+
                          " </div>"+
                    " </div>"+
                      "<div class='col-md-2 col-lg-2' style='float:left;'>"+
                            "<div class='form-group'>"+
                              "<label for='inputInterface'>实际完成时间：</label>"+
                            " <input type='text' name='inputInterface'  class='form-control' value="+item.actualEndTime+">"+
                            "</div>"+
                      "</div>"+
                      "<div class='col-md-2 col-lg-2' style='float:left;'>"+
                            "<div class='form-group'>"+
                              "<label for='inputInterface'>完成情况描述：</label>"+
                              "<input type='text' name='inputInterface'  class='form-control' value="+item.describe+">"+
                            "</div>"+
                      "</div>"+
              " </div>"
            
           
            console.log(item);
          })
         
          layer.open({
            type: 1 //Page层类型
            ,area: ['1400px', '700px']
            ,title: "任务节点信息"
            ,shade: 0.4 //遮罩透明度
            ,maxmin: false //允许全屏最小化
            ,anim: 1 //0-6的动画形式，-1不开启
            ,content: `
              <div id="nodeInfo" style="height:647px;">
              
              </div>  
            `
            ,success: function(layero, index){
        
              $("#nodeInfo").append(str);
            }
          })


        
      }
  });
}